package cn.sdstudio.ai.controller;

import cn.sdstudio.ai.respository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import static cn.sdstudio.enums.StorageType.CHAT;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
@Slf4j
public class ChatController {

        private final ChatClient chatClient;

        private final ChatHistoryRepository chatHistoryRepository;
        @GetMapping(value = "/chat",produces = "text/html;charset=utf-8")
        public Flux<String> chat(@RequestParam(name = "prompt") String prompt,String chatId){
                //保存会话id
                chatHistoryRepository.save(CHAT.getTypeName(),chatId);
                return chatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
    }


}
